/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package flight.model;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tam
 */

public class FlightTextDataSource implements FlightDataSource{

    private BufferedReader reader;
    public FlightTextDataSource(String sourceFile) throws FileNotFoundException{
        reader = new BufferedReader(new FileReader(sourceFile));
    }

    public int getNoOfFlights() {
        /*
         * get total number of flights
         */
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Flight> getAll() {
        try {
            List<Flight> flights = new ArrayList<Flight>();
            String aLine;
            while ((aLine = reader.readLine()) != null) {
                Flight f = new Flight();
                f.id = Integer.parseInt(aLine);
                f.source = reader.readLine();
                f.destination = reader.readLine();
                f.time = Long.parseLong(reader.readLine());
                f.airfare = Float.parseFloat(reader.readLine());
                f.noOfSeats = Integer.parseInt(reader.readLine());
                flights.add(f);
            }
            return flights;
        } catch (IOException ex) {
            Logger.getLogger(FlightTextDataSource.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public Flight getAt(int order) {
        /*
         * random access flight by order
         */
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Flight getById(int id) {
        /*
         * look for the flight by id - not efficient in non-index text
         */
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
